package com.gdit.zax.utils;

import java.io.File;
import java.util.ArrayList;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

import com.gdit.zax.Constant;
import com.gdit.zax.dao.GroupDao;
import com.gdit.zax.dao.SchoolDao;
import com.gdit.zax.dao.UserDao;
import com.gdit.zax.entitys.BaseEntity;
import com.gdit.zax.entitys.Group;
import com.gdit.zax.entitys.UserInfo;
import com.gdit.zax.factory.DAOFactory;

public class XlsUtil {
	public static ArrayList<UserInfo> getAddUserInfo(DAOFactory dao,File xlsfile){
		String schoolname = xlsfile.getName().substring(0, xlsfile.getName().length()-4);
		SchoolDao sdao = dao.getSchoolDao();
		GroupDao gdao = dao.getGroupDao();
		UserDao udao = dao.getUserDao();
		int startuid = udao.getRegisterId();
		//获取登陆的用户
		String token = webUtil.getCookie(dao.getRequest(), "LoginCookie");
		UserInfo info = udao.getInfo(Constant.LOGINTYPE_WEB, token);
		
		//处理学校

		
		if (!sdao.isSchoolExist(schoolname)) {
			sdao.addSchool(schoolname);
		}
		BaseEntity schoolinfo = sdao.getSchoolInfo(schoolname);
		if (schoolinfo==null) {
			return null;
		}
		
		try {
			Workbook wb = Workbook.getWorkbook(xlsfile);
			Sheet[] shyeet = wb.getSheets();
			ArrayList<UserInfo> list = new ArrayList<UserInfo>();
			for (Sheet sheet : shyeet) {
				
				//处理班级
				Group clas = gdao.getGroupInfo(schoolinfo.getId(), sheet.getName());
				if (clas==null) {
					clas = new Group();
					clas.setName(sheet.getName());
					clas.setSchoolid(schoolinfo.getId());
					if (info!=null) {
						clas.setCreateid(info.getId());						
					}
					clas.setId(gdao.addGroup(clas));
				}
				
				
				System.out.println(sheet.getName());
				int rows = sheet.getRows();//行
				int cols = sheet.getColumns();//列
				Cell cells[][] = new Cell[rows][cols];
				for (int i = 0; i < rows; i++) {//行
					boolean iseffect = true;//是否有效
					UserInfo uinfo = new UserInfo();
					uinfo.setPassword("123456");
					uinfo.setSchoolid(schoolinfo.getId());
					uinfo.setId(startuid + list.size());
					
					gdao.addGroup(uinfo.getId(), clas.getId());//猜测的用户id，让他加入该班级
					
					for (int j = 0; j < cols; j++) {
						try {
							cells[i][j] = sheet.getCell(j,i);//getCell(列，行)		
						} catch (Exception e) {
							iseffect = false;
						}
						
						if (cells[i][j]!=null) {
//							System.out.println("行:" + i + " 列:" + j);
//							System.out.println(cells[i][j].getContents());
							
							if (i!=0) {
								String value = cells[i][j].getContents();
								if (j==0 && (value == null || value.trim().equals(""))) {
									iseffect = false;
									continue;
								}
								switch (j) {
								case 0:
									uinfo.setUsername(value);
									break;
								case 1:
									uinfo.setNick(value);
									break;
								case 2:
									int sex = 0;
									if (value.equals("女")) {
										sex=1;
									}
									uinfo.setSex(sex);
									break;
								case 3:
									int age = TextUtil.String2int(value, 0);
									uinfo.setAge(age);
									break;
								case 4:
									uinfo.setPhone(value);
									break;
								case 5:
									uinfo.setAddress(value);
									break;
									
								default:
									break;
								}
								
							} else {
								//第一行，无效
								iseffect = false;
							}
						}
						
					}
					if (iseffect) {
						list.add(uinfo);						
					}
				}
			}
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;//文件出错
	}
}
